#include "common.h"
namespace common {

int search(std::vector<int> &nums, int target) {
    int n = nums.size();
    int left = 0;
    int right = n - 1;
    int mid = 0;
    while (left < right) {
        mid = left + ((right - left) >> 1);
        if (nums[mid] >= nums[right])
            left = mid + 1;
        else {
            right = mid - 1;
        }
    }
    int rot;
    int result = -1;
    if (nums[0] > target) {
        rot = left;
        while (nums[rot] <= target && rot < n - 1) {
            if (nums[rot] == target) {
                result = rot;
            }
            rot++;
        }
    } else {
        rot = 0;
        while (nums[rot] <= target) {
            if (nums[rot] == target && rot < left) {
                result = rot;
            }
            rot++;
        }
    }
    return result;
}
} // namespace common
